package com.slkj.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.slkj.mapper.DocUserInfoMapper;
import com.slkj.pojo.DocUserInfo;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class DocUserInfoService {

    @Resource
    private DocUserInfoMapper userInfoMapper;

    //查询：所有
    public List<DocUserInfo> findAllUserInfo(){ return  userInfoMapper.selectAll(); }

    //多条件查询
    public Page<DocUserInfo> findUserInfoList(Map<String,Object> searchMap, Integer page, Integer pageSize){
        PageHelper.startPage(page,pageSize);
        Example example = createExample(searchMap);
        return  (Page<DocUserInfo>)userInfoMapper.selectByExample(example);
    }

    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(DocUserInfo.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            //单位名称
            if (searchMap.get("name") != null) {
                criteria.andEqualTo("name", searchMap.get("name"));
            }
            //哪个  局
            if (searchMap.get("bureau") != null) {
                criteria.andEqualTo("bureau", searchMap.get("bureau"));
            }
            //局的类别
            if (searchMap.get("bureauType") != null) {
                criteria.andEqualTo("bureauType", searchMap.get("bureauType"));
            }
            //局的编号
            if (searchMap.get("bureauNum") != null) {
                criteria.andEqualTo("bureauNum", searchMap.get("bureauNum"));
            }
            //局的级别 5位：政府领导10001，绩效办公室20002，3考核单位成员，4被考核单位成员，5报送材料单位，6专项行动被考核单位
            if (searchMap.get("bureauLevel") != null) {
                criteria.andEqualTo("bureauLevel", searchMap.get("bureauLevel"));
            }
        }
        return example;
    }

    /**
     * 不显示领导，不可选择为1的领导
     * @param page
     * @param pageSize
     * @return
     */
    public Page<DocUserInfo> findUserNOLeader( Integer page, Integer pageSize){
        PageHelper.startPage(page,pageSize);
        Example example = new Example(DocUserInfo.class);
        Example.Criteria criteria = example.createCriteria();
        List<String> list=new ArrayList<>();
        list.add("1");
        Iterable iterable=list;
        criteria.andNotIn("bureauLevel",iterable);
        return (Page<DocUserInfo>)userInfoMapper.selectByExample(example);
    }


    public DocUserInfo findByBureauNum(Integer bureauNum){
        Example example = new Example(DocUserInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("bureauNum",bureauNum);
       return userInfoMapper.selectOneByExample(example);
    }

}
